package com.toocms.jinjuhang.ui.index.news

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.data.system.CateItemData
import com.toocms.jinjuhang.data.system.CateListData
import com.toocms.jinjuhang.data.system.NewsListData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.event.SingleLiveEvent

class NewsModel(application: Application, val bundle: Bundle?) :
    BaseViewModel<BaseModel>(application) {
    var p = 1
    val type by lazy {
        bundle?.getString(Constants.BundleKey.KEY_TYPE, Constants.Extra.NEWS_TYPE_NEWS)
            ?: Constants.Extra.NEWS_TYPE_NEWS
    }
    var cateId: String? = null
        set(value) {
            field = value
            refresh()
        }
    val filtersSingleLiveEvent = SingleLiveEvent<List<CateItemData>>()
    val items = ObservableArrayList<MultiItemViewModel<*>>()
    val itemBinding = ItemBinding.of<MultiItemViewModel<*>>({ binding, _, item ->
        when (item.itemType) {
            Constants.RecyclerViewItemType.ITEM_TYPE_ZERO -> binding.set(
                BR.newsBannerItemModel, R.layout.item_news_banner
            )
            Constants.RecyclerViewItemType.ITEM_TYPE_ONE -> binding.set(
                BR.newsItemModel, R.layout.item_news
            )
        }
    })


    val stopRefreshOrLoadSingleLiveEvent = SingleLiveEvent<Unit>()

    val refreshBindingCommand = BindingCommand<BindingAction>(BindingAction {
        refresh()
    })

    val loadBindingCommand = BindingCommand<BindingAction>(BindingAction {
        load()
    })

    init {
        cate()
    }


    private fun refresh(isShowLoading: Boolean = false) {
        p = 1
        requestData(p, isShowLoading)
    }

    private fun load() {
        p++
        requestData(p)
    }

    private fun cate() {
        val url = when (type) {
            Constants.Extra.NEWS_TYPE_NEWS -> Urls.System.CATE_LIST
            Constants.Extra.NEWS_TYPE_ACTIVE -> Urls.System.ACTIVE_ZONE
            else -> Urls.System.CATE_LIST
        }
        requestData(url, resultClass = CateListData::class.java, result = {
            filtersSingleLiveEvent.value = it.list ?: return@requestData
        })
    }

    private fun requestData(p: Int, isShowLoading: Boolean = false) {
        val params = mutableMapOf<String, String?>()
        val url: String
        when (type) {
            Constants.Extra.NEWS_TYPE_NEWS -> {
                url = Urls.System.NEWS_LIST
                params["cate_id"] = cateId
            }
            Constants.Extra.NEWS_TYPE_ACTIVE -> {
                url = Urls.System.ACTIVE_ARTICLE
                params["az_id"] = cateId
            }
            else -> {
                url = Urls.System.NEWS_LIST
                params["cate_id"] = cateId
            }
        }
        params["p"] = p.toString()
        requestData(
            url,
            params,
            resultClass = NewsListData::class.java,
            isShowLoading = isShowLoading,
            result = {
                if (1 == p) items.clear()
                it.list?.forEach {
                    items += NewsItemModel(this, ObservableField(it))
                }
            },
            finish = {
                stopRefreshOrLoadSingleLiveEvent.call()
            })
    }


}